
var vTest={
    //验证类型
    type:{
        len:"text",
        fno:"text",
        sid:"text",
        fmail:"text",
        mobile:"number",
        date:"date",
        month:"month",
        time:"time",
        file:"file",
        password:"password",
        number:"number"
    },
    //长度验证
    len:function(elem){
        // console.log(elem.value);
        error=elem.parentNode.parentNode.querySelector(".vtest_error")?elem.parentNode.parentNode.querySelector(".vtest_error"):elem.parentNode.parentNode.querySelector(".error");
        // console.log(error);
        val=elem?elem.value:"";
        min=elem.getAttribute("min")?elem.getAttribute("min"):0;
        max=elem.getAttribute("max")?elem.getAttribute("max"):0;
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(min==0 && max==0){
            error.innerText="";
            return true;
        }
        if(val.length<min || val.length>max){
            error.innerText="字数应该在"+min+"~"+max+"之间";
            return false;
        }
        error.innerText="";
        return true;
    },
    //密码验证
    password:function(elem){
        vTest.len(elem);
    },
    //工号验证
    fno:function(elem){
        error=elem.parentNode.parentNode.querySelector(".vtest_error")?elem.parentNode.parentNode.querySelector(".vtest_error"):elem.parentNode.parentNode.querySelector(".error");
        val=elem?elem.value:"";
        patt=new RegExp(/^(([A-Z0-9a-z]|[\u4e00-\u9fa5]{1,4}){4,10})|(\d{18})$/);
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(!patt.test(elem.value)){
            error.innerText="工号格式不正确";
            return false;
        }
        error.innerText="";
        return true;
    },
    //公司mail验证
    fmail:function(elem){
        error=elem.parentNode.parentNode.querySelector(".vtest_error")?elem.parentNode.parentNode.querySelector(".vtest_error"):elem.parentNode.parentNode.querySelector(".error");
        val=elem?elem.value:"";
        patt=new RegExp(/^([a-zA-Z]|[0-9])(\w|\-)+@(mail.foxconn.com)|(foxconn.com)$/);
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(!patt.test(elem.value)){
            error.innerText="公司邮箱格式错误，必须带有mail.foxconn.com或foxconn.com";
            return false;
        }
        error.innerText="";
        return true;
    },
    //通用邮箱验证
    mail:function(elem){
        error=elem.parentNode.parentNode.querySelector(".vtest_error")?elem.parentNode.parentNode.querySelector(".vtest_error"):elem.parentNode.parentNode.querySelector(".error");
        val=elem?elem.value:"";
        patt=new RegExp(/^([a-zA-Z0-9]+[_|_|\-|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|_|.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,6}$/);
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(!patt.test(elem.value)){
            error.innerText="邮箱格式错误";
            return false;
        }
        error.innerText="";
        return true;
    },
    //验证身份证号
    sid:function(elem){
        error=elem.parentNode.parentNode.querySelector(".vtest_error")?elem.parentNode.parentNode.querySelector(".vtest_error"):elem.parentNode.parentNode.querySelector(".error");
        val=elem?elem.value:"";
        patt=new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/);
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(!patt.test(elem.value)){
            error.innerText="身份证格式错误";
            return false;
        }
        error.innerText="";
        return true;
    },
    //验证手机号
    mobile:function(elem){
        error=elem.parentNode.parentNode.querySelector(".vtest_error")?elem.parentNode.parentNode.querySelector(".vtest_error"):elem.parentNode.parentNode.querySelector(".error");
        val=elem?elem.value:"";
        patt=new RegExp(/^\d{11}$/);
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(!patt.test(elem.value)){
            error.innerText="手机号错误,应该为11位数字";
            return false;
        }
        error.innerText="";
        return true;
    },
    //验证日期格式
    date:function(elem){
        error=elem.parentNode.parentNode.querySelector(".vtest_error")?elem.parentNode.parentNode.querySelector(".vtest_error"):elem.parentNode.parentNode.querySelector(".error");
        val=elem?elem.value:"";
        patt=new RegExp(/^\d{4}\-(0[1-9]|1[0-2])\-(0[1-9]|[1-2][0-9]|3[0-1])$/);
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(!patt.test(elem.value)){
            error.innerText="日期格式错误";
            return false;
        }
        error.innerText="";
        return true;
    },
    //验证月份格式
    month:function(elem){
        error=elem.parentNode.parentNode.querySelector(".vtest_error")?elem.parentNode.parentNode.querySelector(".vtest_error"):elem.parentNode.parentNode.querySelector(".error");
        val=elem?elem.value:"";
        patt=new RegExp(/^\d{4}\-(0[1-9]|1[0-2])$/);
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(!patt.test(elem.value)){
            error.innerText="年月格式错误";
            return false;
        }
        error.innerText="";
        return true;
    },
    //验证时间格式
    time:function(elem){
        error=elem.parentNode.parentNode.querySelector(".vtest_error")?elem.parentNode.parentNode.querySelector(".vtest_error"):elem.parentNode.parentNode.querySelector(".error");
        val=elem?elem.value:"";
        patt=new RegExp(/^([0-1][0-9]|2[0-3]):([0-5][0-9])$/);
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(!patt.test(elem.value)){
            error.innerText="时间格式错误";
            return false;
        }
        error.innerText="";
        return true;
    },
    //验证自动验证
    code:function(elem){
        var check=elem.parentNode.querySelectorAll("input")[1];
        var error=elem.parentNode.parentNode.querySelector(".vtest_error");
        if(vTest.CanNull(elem)){
            error.innerText="";
            return true;
        }
        if(check.value!=elem.value){
            error.innerText="验证码输入错误";
            return false;
        }
        error.innerText="";
        return true;
    },
    //全部验证
    TestAll:function(){
        //输入组件
        var check=0;
        var vinputs=document.querySelectorAll(".vinput_input");
        for(var i=0;i<vinputs.length;i++){
            // console.log(vinputs[i]);
            // console.log(vinputs[i].getAttribute("validate")?vinputs[i].getAttribute("validate"):"len");
            if(!vTest[vinputs[i].getAttribute("validate")?vinputs[i].getAttribute("validate"):"len"](vinputs[i])){
                check++;
            }
        }
        //多行输入组件
        var vtextareas=document.querySelectorAll(".vinput_textarea");
        for(var i=0;i<vtextareas.length;i++){
            // console.log(vtextareas[i]);
            if(!vTest.len(vtextareas[i])){
                check++;
            }
        }
        //多选组件
        var vselects=document.querySelectorAll(".vinput_select");
        for(var i=0;i<vselects.length;i++){
            //console.log(vselects[i]);
            var vlist=vselects[i].querySelectorAll(".vinput_valuelist_item");
            var input=vselects[i].parentNode.querySelectorAll("input")[1];
            var error=vselects[i].parentNode.parentNode.querySelector(".vtest_error");
            //console.log(input);
            if(vlist.length<input.getAttribute("min")){
                error.innerText="至少选择"+input.getAttribute("min")+"个";
                check++;
            }else{
                error.innerText="";
            }
        }
        // console.log(check);
        if(check>0){
            // console.log(check);
            return false;
        }
        return true;
    },
    //单笔验证
    Test:function(){
        var validate=this.getAttribute("validate")?this.getAttribute("validate"):"len";
        console.log(this);
        vTest[validate](this);
    },
    //可不填验证
    CanNull:function(elem){
        if(elem.getAttribute("min")==-1 && elem.value==""){
            return true;
        }
        return false;
    }
}
//组件属性模型
var op={
    Init:function(){
        op.id="";
        op.validate="len";
        op.min=0;
        op.max=0;
        op.placeholder="请输入";
        op.value="";
        op.readonly=false;
        op.type=vTest.type[op.validate];
    },
    Get:function(){
        var vop={
            id:op.id,
            validate:op.validate,
            min:op.min,
            max:op.max,
            placeholder:op.placeholder,
            value:op.value,
            readonly:op.readonly,
            onblur:vTest.Test
        }
        return vop;
    },
    Set:function(json){
        op.Init();
        for(var key in json){
            if(typeof op[key]!="undefined" && typeof json[key]!="undefined"){
                op[key]=json[key];
                if(key=="validate"){
                    op.type=vTest.type[op.validate];
                }
            }
        }
    }
}
//输入组件
var vInput={
    view:function(vnode){
        op.Set(vnode.children[0]);
        console.log(op);
        return m("div.vinput",[
            m("div.vinput_line",[
                m("div.vinput_tag",vnode.children[0].tag?vnode.children[0].tag:""),
                m("input[type="+op.type+"].vinput_input",op.Get())
            ]),
            m("div.vtest_error")
        ])
    }
}
//多行输入组件
var vTextarea={
    view:function(vnode){
        op.Set(vnode.children[0]);
        return m("div.vinput",[
            m("div.vinput_line2",[
                m("div.vinput_tag",vnode.children[0].tag?vnode.children[0].tag:""),
                m("textarea.vinput_textarea",op.Get())
            ]),
            m("div.vtest_error")
        ])
    }
}
//单选组件
var vRadio={
    rd:false,
    view:function(vnode){
        vRadio.rd=vnode.children[0].readonly?vnode.children[0].readonly:false;
        return m("div.vinput",[
            m("div.vinput_line",[
                m("div.vinput_tag2",vnode.children[0].tag?vnode.children[0].tag:""),
                vnode.children[0].datalist?
                m("div.vinput_radio",vnode.children[0].datalist.map(function(op){
                    var val=vnode.children[0].value?vnode.children[0].value:vnode.children[0].datalist[0];
                    return op==val?m("div.vinput_radio1",{onclick:vRadio.Select},op):m("div.vinput_radio2",{onclick:vRadio.Select},op);
                }))
                :m("div","没有定义datalist"),
                m("input[type=hidden]",{
                    //id
                    id:vnode.children[0].id?vnode.children[0].id:"",
                    value:vnode.children[0].value?vnode.children[0].value:vnode.children[0].datalist[0]
                })
            ])
        ])
    },
    Select:function(){
        if(vRadio.rd){
            return;
        }
        var radio=this.parentNode.querySelectorAll("div");
        // console.log(radio);
        for(var i=0;i<radio.length;i++){
            radio[i].setAttribute("class","vinput_radio2");
        }
        this.setAttribute("class","vinput_radio1");
        this.parentNode.parentNode.querySelector("input").value=this.innerText;
    }
}
//检索下拉组件
var vSelect={
    valist:[],
    oninit:function(vnode){
        var val=vnode.children[0].value?vnode.children[0].value:"";
        vSelect.valist=val==""?[]:val.split(",");
    },
    view:function(vnode){
        return m("div.vinput",[
            m("div.vinput_line",[
                m("div.vinput_tag",vnode.children[0].tag?vnode.children[0].tag:""),
                m("div.vinput_select",[
                    //检索
                    m("div.vinput_list",[
                        //初始化数值
                        m("div.vinput_valuelist",vSelect.valist.length>0?vSelect.valist.map(function(val){
                            return m("div.vinput_valuelist_item",[
                                m("span",val),
                                m("button",{onclick:vSelect.DelValue},"×")
                            ])
                        }):""),
                        //检索输入
                        m("input[type=text]",{
                            placeholder:"检索",
                            onclick:vSelect.ShowDatalist,
                            oninput:vSelect.FilterDatalist,
                            style:"min-width:20px; background-color: transparent;"
                        })
                    ]),
                    //选择区域
                    m("div.vinput_datalist",[
                        vnode.children[0].datalist.map(function(dvalue){
                            return m("div.vinput_datalist_item",{
                                onclick:vSelect.SelectValue
                            },dvalue)
                        })
                    ])
                ]),
                m("input[type=hidden]",{
                    //id
                    id:vnode.children[0].id?vnode.children[0].id:"",
                    //value
                    value:vnode.children[0].value?vnode.children[0].value:"",
                    //min
                    min:vnode.children[0].min?vnode.children[0].min:0,
                    //max
                    max:vnode.children[0].max?vnode.children[0].max:1,
                })
            ]),
            m("div.vtest_error")
        ])
    },
    //显示数组列
    ShowDatalist:function(){
        var dlist=this.parentNode.parentNode.querySelector(".vinput_datalist");
        dlist.style.display="block";
    },
    //过来数据列
    FilterDatalist:function(){
        var dlist=this.parentNode.parentNode.querySelector(".vinput_datalist").querySelectorAll(".vinput_datalist_item");
        for(var i=0;i<dlist.length;i++){
            if(dlist[i].innerText.indexOf(this.value)>-1){
                dlist[i].style.display="block";
            }else{
                dlist[i].style.display="none";
            }
        }
    },
    //隐藏数据列
    HideDatalist:function(){
        var dlist=this.parentNode.parentNode.querySelector(".vinput_datalist");
        dlist.style.display="none";
    },
    //刷新结果列
    FlushValuelist:function(elem,val){
        vSelect.valist=val.split(",");
        if(val==""){
            vSelect.valist=[];
        }
        m.render(elem,vSelect.valist.map(function(val){
            return m("div.vinput_valuelist_item",[
                m("span",val),
                m("button",{onclick:vSelect.DelValue},"×")
            ])
        }))
    },
    //删除结果列
    DelValue:function(){
        var input=this.parentNode.parentNode.parentNode.parentNode.parentNode.querySelectorAll("input")[1];
        var vlist=this.parentNode.parentNode.parentNode.querySelector(".vinput_valuelist");
        var thisVal=this.parentNode.querySelector("span").innerText;
        var error=this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.querySelector(".vtest_error");
        error.innerText="";
        // console.log(thisVal);
        vSelect.valist=input.value.split(",");
        if(val==""){
            vSelect.valist=[];
        }
        for(var i=0;i<vSelect.valist.length;i++){
            // console.log(vSelect.valist[i]);
            if(thisVal==vSelect.valist[i]){
                vSelect.valist.splice(i,1);
            }
        }
        var val=vSelect.valist.join(",");
        input.value=val;
        vSelect.FlushValuelist(vlist,val);
    },
    //选择数据
    SelectValue:function(){
        // console.log(this);
        var input=this.parentNode.parentNode.parentNode.querySelectorAll("input")[1];
        var dlist=this.parentNode.parentNode.querySelector(".vinput_datalist");
        var vlist=this.parentNode.parentNode.querySelector(".vinput_valuelist");
        var error=this.parentNode.parentNode.parentNode.parentNode.querySelector(".vtest_error");
        // console.log(input);
        dlist.style.display="none";
        if(input.value.indexOf(this.innerText)>-1){
            return;
        }
        if(input.value!="" && input.value.split(",").length>=input.getAttribute("max")){
            error.innerText="最多只能选择"+input.getAttribute("max")+"个";
            return;
        }
        error.innerText="";
        input.value=input.value+(input.value==""?this.innerText:(","+this.innerText));
        vSelect.FlushValuelist(vlist,input.value);
    }
}
//验证组件
var vCode={
    v1:0,v2:0,v3:0,v4:0,
    code:'',
    oninit:function(){
        vCode.v1=parseInt(Math.random()*10);
        vCode.v2=parseInt(Math.random()*10);
        vCode.v3=parseInt(Math.random()*10);
        vCode.v4=parseInt(Math.random()*10);
        vCode.code=vCode.v1.toString()+vCode.v2.toString()+vCode.v3.toString()+vCode.v4.toString();
    },
    view:function(vnode){
        return m("div.vinput",[
            m("div.vinput_line",[
                m("div.vinput_tag",vnode.children[0].tag?vnode.children[0].tag:""),
                m("input[type=text].vinput_input",{
                    //提示
                    placeholder:vnode.children[0].placeholder?vnode.children[0].placeholder:"验证码",
                    //id
                    id:vnode.children[0].id?vnode.children[0].id:"",
                    //validate
                    validate:vnode.children[0].validate?vnode.children[0].validate:"len",
                    onblur:vCode.CheckCode
                }),
                m("input[type=hidden]",{
                    value:vCode.code
                }),
                m("div.vinput_code",[
                    m("div.vinput_code_item",{style:"font-size:"+(parseInt(Math.random())+0.7)+"rem; transform:rotate(-"+parseInt(Math.random()*30)+"deg);"},vCode.v1),
                    m("div.vinput_code_item",{style:"font-size:"+(Math.random()+0.7)+"rem; transform:rotate("+parseInt(Math.random()*30)+"deg);"},vCode.v2),
                    m("div.vinput_code_item",{style:"font-size:"+(Math.random()+0.7)+"rem; transform:rotate(-"+parseInt(Math.random()*30)+"deg);"},vCode.v3),
                    m("div.vinput_code_item",{style:"font-size:"+(Math.random()+0.7)+"rem; transform:rotate("+parseInt(Math.random()*30)+"deg);"},vCode.v4)
                ])
            ]),
            m("div.vtest_error")
        ])
    },
    CheckCode:function(){
        var check=this.parentNode.querySelectorAll("input")[1];
        var error=this.parentNode.parentNode.querySelector(".vtest_error");
        if(check.value!=this.value){
            error.innerText="验证码输入错误";
        }else{
            error.innerText="";
        }
    }
}

// //动态载入css
// var validate_js_dir=mithril_init.GetPath(document.scripts);
// console.log(validate_js_dir);
// var css=document.createElement("link");
// css.rel = "stylesheet";
// css.href=validate_js_dir+"validate.css";
// document.head.append(css);